#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

int init[4][4];

int res[4][4];

bool initvis[4][4];

bool vis[4][4];

int steps = 0xfff;
int tempsteps = 0;
int sum = 0;

int dirx[] = {1, -1, 0, 0};
int diry[] = {0, 0, 1, -1};

vector<pair<int, int>> v;

void dfs(int i, int j)
{

    if (tempsteps > steps)
        return; //已经大于最小，直接剪枝

    if (init[i][j] == res[i][j] && init[i][j] == 1)
    {
        if (tempsteps < steps)
        {
            if (!v.empty())
            {
                auto temp = v.back();
                initvis[temp.first][temp.second] = false;
            }
            v.push_back(make_pair(i, j));
            initvis[i][j] = true;
            steps = tempsteps;
        }

        return;
    }
    vis[i][j] == true;
    for (int k = 0; k < 4; k++)
    {
        int tx = i + dirx[k];
        int ty = j + diry[k];
        if (!vis[tx][ty] && tx>=0 && tx<=3 && ty>=0 && ty<=3)
        {
            tempsteps++;
            init[tx][ty] = 1;
            vis[tx][ty] = true;
            dfs(tx, ty);
            vis[tx][ty] = false;
            tempsteps--;
            init[tx][ty] = 0;
        }
    }
}

int main() 
{
    string s;
    for (int i = 0; i < 4; i++)
    {
        cin >> s;
        for (int j = 0; j < 4; j++)
        {
            init[i][j] = s[j] - '0';
        }
    }

    for (int i = 0; i < 4; i++)
    {
        cin >> s;
        for (int j = 0; j < 4; j++)
        {
            res[i][j] = s[j] - '0';
            if (res[i][j] == init[i][j] && res[i][j] == 1)
                initvis[i][j] = true;
        }
    }

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            memcpy(vis, initvis, sizeof(initvis));
            if (!initvis[i][j] && init[i][j]==1)
            {
                steps = 0xfff;
                tempsteps = 0;
                v.clear();
                dfs(i, j);
                sum += steps;
            }
        }
    }
    cout << sum << endl;
    system("pause");
    return 0;
}